iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 12
0
自我挑戰組

暫時停止遊戲,寫遊戲!系列 第 12

[Day12] 1A2B猜數字遊戲 Part1.繪出遊戲場景

  • 分享至 

  • xImage
  •  

祝大家耶誕快樂


開啟新專案與繪出遊戲起始場景方法皆相同,這裡不再詳細說明,如過程中有問題請參考[Day03] 簡介Visual Studio +XNA 與 [Day05] 剪刀石頭布猜拳遊戲Part1.繪出遊戲場景這兩篇文章

目標

  • 新增XNA專案
  • 載入遊戲所需資源檔案
  • 繪出場景,顯示開始畫面

可學到的東西

  • 載入遊戲資源
  • 繪圖函數 sprite

資源:

目標實作: 新增XNA專案

檔案 > 新增專案 > 範本 > Visual C# > Windows Game (4.0),將遊戲命名名為 GuessNumber1A2BMoocs

目標實作: 載入遊戲資源

在遊戲直接調用資源都要先載入到開發環境中,不然有有可能出現錯誤,請特別注意。
這裡我們先載入開始畫面以及之後要用到的圖片。

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
 
    // 替每張圖片宣告一個變數
    Texture2D imageNumber, imageGuess, imagePassGame;
  • 在 LoadContent() 執行階段載入資源
    把上面宣告的變數,一一對應檔案名稱後載入,
    注意! 檔案名稱是不需要副檔名的
protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
 
    // TODO: use this.Content to load your game content here
    // 載入變數,並對應每個圖片變數
    imageNumber = Content.Load<Texture2D>("number640x256");
    imageGuess = Content.Load<Texture2D>("guess256x128");
    imagePassGame = Content.Load<Texture2D>("retry800x600");
}

目標實作: 繪出場景

在遊戲中要繪製圖型、字型等,只需要XNA提到的Sprite 即可,
不只可顯示於畫面上,還可以對圖片做放大、縮小等功能。
此 Sprite 是遊戲專案內建中,就是一開始就幫我們宣告的 SpriteBatch spriteBatch;

  • 先在 Initialize() 執行階段初始化畫面大小
protected override void Initialize()
{
    // TODO: Add your initialization logic here
    // 設定起始畫面大小
    graphics.PreferredBackBufferWidth = 800;
    graphics.PreferredBackBufferHeight = 600;
    graphics.ApplyChanges();
 
    base.Initialize();
}
  • 另外撰寫繪圖函數 drawing()
    由於之後的遊戲在繪製畫面開始變得複雜,這裡我們獨立出來一個函數以使程式碼簡潔
void drawing(SpriteBatch spriteBatch)
{
    spriteBatch.Begin();
    // sprite.draw 的使用方法很多種,這裡載入的只有3個參數
    // 參數1表示要顯示的圖片,參數2表示顯示圖片的起始位置,參數3表示填色,White表示不填色
    spriteBatch.Draw(imageNumber, Vector2.Zero, Color.White);
    spriteBatch.Draw(imageGuess, new Vector2(400,400), Color.White);
    spriteBatch.End();
}
  • 在 Draw() 執行階段呼叫drawing()函數
    還有一個小變動是把預設背景改成 DarViolet
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.DarkViolet);
 
    // TODO: Add your drawing code here
    drawing(spriteBatch);
 
    base.Draw(gameTime);
}
  • 按下功能表的開始,執行看看是否成功

Source Code

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
 
namespace GuessNumber1A2BMoocs
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
 
        // 替每張圖片宣告一個變數
        Texture2D imageNumber, imageGuess, imagePassGame;
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
 
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            // 設定起始畫面大小
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ApplyChanges();
 
            base.Initialize();
        }
 
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
 
            // TODO: use this.Content to load your game content here
            // 載入變數,並對應每個圖片變數
            imageNumber = Content.Load<Texture2D>("number640x256");
            imageGuess = Content.Load<Texture2D>("guess256x128");
            imagePassGame = Content.Load<Texture2D>("retry800x600");
        }
 
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
 
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
 
            // TODO: Add your update logic here
 
            base.Update(gameTime);
        }
 
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkViolet);
 
            // TODO: Add your drawing code here
            drawing(spriteBatch);
 
            base.Draw(gameTime);
        }
        void drawing(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            // sprite.draw 的使用方法很多種,這裡載入的只有3個參數
            // 參數1表示要顯示的圖片,參數2表示顯示圖片的起始位置,參數3表示填色,White表示不填色
            spriteBatch.Draw(imageNumber, Vector2.Zero, Color.White);
            spriteBatch.Draw(imageGuess, new Vector2(400,400), Color.White);
            spriteBatch.End();
        }
    }
}

上一篇
[Day11] 1A2B猜數字遊戲
下一篇
明日開始準備連載
系列文
暫時停止遊戲,寫遊戲!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
虎虎
iT邦研究生 5 級 ‧ 2016-12-27 23:33:24

好哦 魯大常大大耶誕快樂!!!!!

魯大常 iT邦新手 3 級 ‧ 2016-12-27 23:39:07 檢舉

過了哦,謝謝您 =v=

我要留言

立即登入留言